﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;
using System.Text;
using System.Web.Script;
using System.Drawing;
using System.Data;

using BOMS.DAL;
using BOMS.DBUtility;
using BOMS.Model;
using BOMS.Utility;

namespace BOMS.WebUI.Web
{
    public partial class Message : System.Web.UI.Page
    {
        const string JSON_FIELD_FORMAT = "\"{0}\":\"{1}\",";
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                 //注册AjaxPro
                AjaxPro.Utility.RegisterTypeForAjax(typeof(Message), Page);
               
                EquipMessageInfo info = this.EquipStatusCount();                
                //掉线
                lblOfflineCount.Text = info.OffLineCount.ToString();
                //在线
                lblOnLineCount.Text = info.OnLineCount.ToString();
                //停电的主机数量
                lblPowerDown.Text = info.PowerDownCount.ToString();
                //报警的主机数量
                lblWarning.Text = info.WarningCount.ToString();
                //全网主机数量
                lblAllEquipCount.Text = info.AllEquipCount.ToString();
                
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [AjaxMethod]
        public string GetMessagePageJson()
        {
            /*
            string jsonRet;
            StringBuilder sb = new StringBuilder("({");
            EquipStatusCount(out jsonRet);//调用方法，out出json内容
            sb.AppendFormat(JSON_FIELD_FORMAT, "OnLineCount", nOnline);
            sb.AppendFormat(JSON_FIELD_FORMAT, "PowerDownCount", nPowerdown);
            sb.AppendFormat(JSON_FIELD_FORMAT, "WarningCount", nWarning);
            sb.AppendFormat(JSON_FIELD_FORMAT, "AllEquipCount", (nOffLine + nOnline));
            sb.AppendFormat(JSON_FIELD_FORMAT, "OfflineCount", nOffLine);
            sb.Length--; //除逗号

            sb.Append("})");
            jsonRet = sb.ToString(); */
            EquipMessageInfo info = this.EquipStatusCount();
            string json = JavaScriptSerializer.Serialize(info);
            return json;
        }
        EquipMessageInfo EquipStatusCount()
        {

            //DataSet ds = new DataSet();
            //ds = RealDate.getWinceInfoOfLan();

            DataSet ds =  RealDate.getWinceInfoOfLan();

            DataTable dt = ds.Tables[0];
            int nOnline = 0;
            int nOffLine = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                 DateTime LastTime;
                if (DbHelper.DBType == ConstSet.DBTYPE_MYSQL)
                {
                    LastTime = ((MySql.Data.Types.MySqlDateTime)dt.Rows[i]["LatestTime"]).GetDateTime();
                }
                else
                {
                    LastTime = (DateTime)dt.Rows[i]["LatestTime"];
                }
                TimeSpan ts = new TimeSpan(0, 3, 0);//3分钟；
                if (DateTime.Now > LastTime.Add(ts))//已超时
                {
                    nOffLine++;
                }
                else
                {
                    nOnline++;
                }
            }

            int nPowerdown = RealDate.getPowerDownEquipCount();
            int nWarning = RealDate.getWarningEquipCount();
            EquipMessageInfo emi = new EquipMessageInfo();
            emi.OffLineCount = nOffLine;
            emi.OnLineCount = nOnline;
            emi.PowerDownCount = nPowerdown;
            emi.WarningCount = nWarning;
            emi.AllEquipCount = nOffLine + nOnline;
            return emi;
        }
        
    }

}